package com.seezoon.application.tcp.executor.press.cases;

import com.seezoon.infrastructure.tcp.codec.JT808;
import com.seezoon.infrastructure.tcp.codec.JTMessage;
import com.seezoon.infrastructure.tcp.codec.JTSerialization;
import com.seezoon.infrastructure.tcp.handler.DispatcherHandler;
import com.seezoon.infrastructure.tcp.handler.MessageHandler;
import com.seezoon.infrastructure.tcp.jt808.JT0100;
import com.seezoon.infrastructure.tcp.jt808.JT0102;
import com.seezoon.infrastructure.tcp.jt808.JT8001;
import com.seezoon.infrastructure.tcp.jt808.JT8100;
import com.seezoon.infrastructure.tcp.transport.NettyClient;
import io.netty.buffer.Unpooled;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;

@Slf4j
@State(Scope.Benchmark)
public class Auth {

    protected NettyClient client;
    protected String serialNo = RandomStringUtils.insecure().nextNumeric(11);

    /**
     * Level.Trial 一个进程一个
     */
    @Setup(Level.Trial)
    public void setUp() {
        Map<Integer, MessageHandler> handlerMap = new HashMap<>();
        DispatcherHandler dispatcherHandler = new DispatcherHandler(handlerMap);
        client = new NettyClient("43.163.9.111", 8000, dispatcherHandler);
        client.connect();
        String token = this.register();
        this.auth(token);
    }

    @TearDown(Level.Trial)
    public void tearDown() {
        client.stop();
    }

    private void auth(String token) {
        JT0102 request = new JT0102();
        request.setAuthKey(token);
        byte[] encode = JTSerialization.encode(request);
        JTMessage jtMessage = new JTMessage();
        jtMessage.setMsgId(JT808.终端鉴权);
        jtMessage.setMsgProperties(encode.length);
        jtMessage.setSerialNo(serialNo);
        jtMessage.setMsgSerialNo(client.nextSequence(serialNo));
        jtMessage.setBody(Unpooled.wrappedBuffer(encode));
        JT8001 response = client.send(jtMessage, JT8001.class);
        log.info("auth response:{}", response);

    }

    private String register() {
        // 注册
        JT0100 request = new JT0100();
        request.setProvinceId(11);
        request.setCityId(1101);
        request.setManufacturerId("12345");
        request.setDeviceModel("RXTmt2503d32m");
        // 放空
        request.setDeviceNo(null);
        request.setPlateColor((short) 1);
        request.setPlateNo("京A12345");

        byte[] encode = JTSerialization.encode(request);
        JTMessage jtMessage = new JTMessage();
        jtMessage.setMsgId(JT808.终端注册);
        jtMessage.setMsgProperties(encode.length);
        jtMessage.setSerialNo(serialNo);
        jtMessage.setMsgSerialNo(client.nextSequence(serialNo));
        jtMessage.setBody(Unpooled.wrappedBuffer(encode));
        JT8100 response = client.send(jtMessage, JT8100.class);
        log.info("register response:{}", response);
        return response.getToken();
    }
}
